#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @Time      :2021/10/9 8:24
# @Author    :cjw
from httprunner import HttpRunner, Config, Step, RunRequest, RunTestCase
from testcases.login_test import TestLogin as Login


class TestCreateDoc(HttpRunner):
	config = Config("创建文件").verify(False).variables(**{
		"memberId": "7301493673162613",
		"data_unique_id": "${get_unique_id()}",
		"folderId": "1SPp0ofUxlp"
	})

	teststeps = [
		Step(
			RunTestCase('login mubu')
				.call(Login)
				.export("token")
		),
		Step(
			RunRequest("/v3/api/list/create_doc")
				.options("https://api2.mubu.com/v3/api/list/create_doc")
				.with_headers(
				**{
					"accept": "*/*",
					"access-control-request-method": "POST",
					"access-control-request-headers": "content-type,data-unique-id,jwt-token,version,x-request-id",
					"origin": "https://mubu.com",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"sec-fetch-mode": "cors",
					"sec-fetch-site": "same-site",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.validate()
				.assert_equal("status_code", 200)
		),
		Step(
			RunRequest("/v3/api/list/create_doc")
				.with_variables(**{
				"sleep_secs": "${random_int()}"
			})
				.setup_hook("${check_sleep_seconds($sleep_secs)}")
				.post("https://api2.mubu.com/v3/api/list/create_doc")
				.with_headers(
				**{
					"content-length": "35",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"version": "3.0.0-2.0.0.1824",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"folderId": "$folderId", "type": 0})
				.extract()
				.with_jmespath('body.data.id', 'docId')
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/list/item_count")
				.post("https://api2.mubu.com/v3/api/list/item_count")
				.with_headers(
				**{
					"content-length": "30",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"version": "3.0.0-2.0.0.1824",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"folderId": 0, "source": "home"})
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/document/edit/get")
				.post("https://api2.mubu.com/v3/api/document/edit/get")
				.with_headers(
				**{
					"content-length": "37",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"version": "3.0.0-2.0.0.1824",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"docId": "$docId", "password": ""})
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/register")
				.get("https://api2.mubu.com/v3/api/colla/register")
				.with_headers(
				**{
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "94",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"reqId": "1",
					"type": "USER_HEARTBEAT",
					"memberId": "$memberId",
					"documentId": "$docId",
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/refer/doc/list")
				.post("https://api2.mubu.com/v3/api/refer/doc/list")
				.with_headers(
				**{
					"content-length": "29",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"targetDocId": "$docId"})
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/refer/node/count")
				.post("https://api2.mubu.com/v3/api/refer/node/count")
				.with_headers(
				**{
					"content-length": "29",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"version": "3.0.0-2.0.0.1824",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"targetDocId": "$docId"})
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/members_v2")
				.post("https://api2.mubu.com/v3/api/colla/members_v2")
				.with_headers(
				**{
					"content-length": "58",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"request-id": "members:$memberId:1633660915267",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"memberId": "$memberId", "documentId": "$docId"})
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)

		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "197",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"memberId": "$memberId",
					"type": "CHANGE",
					"version": 0,
					"documentId": "$docId",
					"events": [
						{"name": "nameChanged", "title": "d", "original": ""},
						{"name": "nameChanged", "title": "de", "original": "d"},
					],
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "205",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"memberId": "$memberId",
					"type": "CHANGE",
					"version": 1,
					"documentId": "$docId",
					"events": [
						{"name": "nameChanged", "title": "dem", "original": "de"},
						{"name": "nameChanged", "title": "demo", "original": "dem"},
					],
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/refer/search_refers")
				.post("https://api2.mubu.com/v3/api/refer/search_refers")
				.with_headers(
				**{
					"content-length": "52",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json({"docId": "$docId", "keywords": "demo", "option": 1})
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "250",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"memberId": "$memberId",
					"type": "CHANGE",
					"version": 2,
					"documentId": "$docId",
					"events": [
						{
							"name": "create",
							"created": [
								{
									"index": 0,
									"parentId": None,
									"node": {
										"id": "CLuI1bi5MG",
										"text": "",
										"modified": 1633660922039,
										"children": [],
									},
									"path": ["nodes", 0],
								}
							],
						}
					],
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "295",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"memberId": "$memberId",
					"type": "CHANGE",
					"version": 3,
					"documentId": "$docId",
					"events": [
						{
							"name": "update",
							"updated": [
								{
									"updated": {
										"id": "CLuI1bi5MG",
										"text": "<span>111</span>",
										"modified": 1633660924229,
									},
									"original": {
										"id": "CLuI1bi5MG",
										"text": "",
										"modified": 1633660922039,
									},
									"path": ["nodes", 0],
								}
							],
						}
					],
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "250",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"memberId": "$memberId",
					"type": "CHANGE",
					"version": 4,
					"documentId": "$docId",
					"events": [
						{
							"name": "create",
							"created": [
								{
									"index": 1,
									"parentId": None,
									"node": {
										"id": "L32AmDhggY",
										"text": "",
										"modified": 1633660925108,
										"children": [],
									},
									"path": ["nodes", 1],
								}
							],
						}
					],
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
		Step(
			RunRequest("/v3/api/colla/events")
				.post("https://api2.mubu.com/v3/api/colla/events")
				.with_headers(
				**{
					"content-length": "295",
					"sec-ch-ua": '" Not;A Brand";v="99", "Google Chrome";v="91", "Chromium";v="91"',
					"sec-ch-ua-mobile": "?0",
					"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.114 Safari/537.36",
					"data-unique-id": "$data_unique_id",
					"content-type": "application/json;charset=UTF-8",
					"accept": "application/json, text/plain, */*",
					"jwt-token": "$token",
					"x-request-id": "${get_unique_request_id()}",
					"origin": "https://mubu.com",
					"sec-fetch-site": "same-site",
					"sec-fetch-mode": "cors",
					"sec-fetch-dest": "empty",
					"referer": "https://mubu.com/",
					"accept-encoding": "gzip, deflate, br",
					"accept-language": "zh-CN,zh;q=0.9",
				}
			)
				.with_json(
				{
					"memberId": "$memberId",
					"type": "CHANGE",
					"version": 5,
					"documentId": "$docId",
					"events": [
						{
							"name": "update",
							"updated": [
								{
									"updated": {
										"id": "L32AmDhggY",
										"text": "<span>333</span>",
										"modified": 1633660926164,
									},
									"original": {
										"id": "L32AmDhggY",
										"text": "",
										"modified": 1633660925108,
									},
									"path": ["nodes", 1],
								}
							],
						}
					],
				}
			)
				.validate()
				.assert_equal("status_code", 200)
				.assert_equal("body.code", 0)
		),
	]


if __name__ == "__main__":
	TestCreateDoc().test_start()
